package com.jfinal.ext.kit;

import com.jfinal.kit.PropKit;
import com.fysports.web.api.MobileType;
import com.fysports.web.config.AppConst;
import com.fysports.web.kit.commons.CookiesKit;
import com.fysports.web.kit.commons.WebContextKit;
import com.fysports.web.model.system.SystemUser;

import javax.servlet.http.HttpServletRequest;


/**
 * Class title:Request 包装类<br/>
 * Describe:封装request基本参数,并包含基本业务参数和业务判断<br/>
 * Created by konbluesky<br/>
 * Date : 15/11/27 下午12:30<br/>
 * Project : jdmdataServer<br/>
 */
public class RequestWarpper {
    private HttpServletRequest request;

    public RequestWarpper(HttpServletRequest request) {
        this.request = request;
    }


    public boolean isAjax() {
        return "XMLHttpRequest".equals(request.getHeader("X-Requested-With"));
    }


    /**
     * 是否为移动端访问接口
     * 
     * @return
     */
    public boolean isMobileReq() {
        // 路径:/app.server/api/xx/xx
        String url = WebContextKit.getRequestURIWithParam(request);
        String appname = (String) request.getAttribute("CONTEXT_PATH");
        url = url.replace(appname, "");
        String url_mobile = PropKit.use(AppConst.CONFIG_SYSTEM).get(AppConst.URL_MOBILE,"");
        if (url.startsWith(url_mobile)) {
            return true;
        }
        return false;
    }


    /**
     * 是否为静态文件
     * @return
     */
    public boolean isStaticRes(){
        return false;
    }


    /**
     * 获取移动设备类型
     * @return
     */
    public MobileType getMType(){
        String agent=request.getHeader("User-Agent");
        return MobileType.get(agent);
    }


    /**
     * 获取系统用户
     * @return
     */
    public SystemUser getSysUser(){
        SystemUser su=CookiesKit.getSystemUser(request);
        return su;
    }

}
